﻿using System;
using System.Collections.Generic;
using Epitech.Krisk.Xna.ScreenManagerLib.ScreenManager;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;

namespace Epitech.Krisk.Xna.ScreenManagerLib.Widget
{
    public class Slider : MenuEntry
    {
        List<String> elements;
        public int curr { get; set; }

        public List<String> Elements
        {
            get { return elements; }
            set { elements = value; }
        }

        public Slider(List<string> elems, int currentelem = 0)
            : base(String.Format("{0,3}", elems[currentelem]))
        {
            curr = currentelem;
            elements = elems;
        }

        public Slider(int max, int current = 0)
            : base(String.Format("{0,3}", current))
        {
            curr = current;
            elements = new List<string>();
            for (int i = 0; i <= max; i++)
            {
                elements.Add(i.ToString());
            }
        }

        public Slider(int min, int max, int current = 0)
            : base(String.Format("{0,3}", current))
        {
            elements = new List<string>();
            for (int i = min; i <= max; i++)
            {
                elements.Add(i.ToString());
            }
        }

        DateTime slidespeed = DateTime.Now;
        private Texture2D _left;
        private Texture2D _right;

        public override void LoadContent(GameScreen currentScreen)
        {
            _left = currentScreen.ScreenManager.Game.Content.Load<Texture2D>("Game/Left");
            _right = currentScreen.ScreenManager.Game.Content.Load<Texture2D>("Game/Right");

            _savex = Position.X;
            base.LoadContent(currentScreen);
        }

        private float _savex;
        private String _lasttext;

        public override void Update(GameScreen screen, bool isSelected, GameTime gameTime)
        {
            base.Update(screen, isSelected, gameTime);
            if (_lasttext != Text)
            {
                Vector2 tmp;

                tmp.X = _savex - screen.ScreenManager.Font.MeasureString(Text).X/2;
                tmp.Y = Position.Y;
                Position = tmp;
            }

            _lasttext = Text;
        }

        public override void HandleInput(GameScreen currentScreen, InputState input)
        {
            if (isSelect)
            {
                PlayerIndex tmp;

                if (input.IsKeyPress(Keys.Left, null, out tmp))
                {
                    if (DateTime.Now - slidespeed > TimeSpan.FromMilliseconds(100))
                    {
                        curr--;
                        slidespeed = DateTime.Now;
                    }
                    if (curr < 0)
                        curr = 0;
                }
                if (input.IsKeyPress(Keys.Right, null, out tmp))
                {
                    if (DateTime.Now - slidespeed > TimeSpan.FromMilliseconds(100))
                    {
                        curr++;
                        slidespeed = DateTime.Now;
                    }
                    if (curr >= elements.Count)
                        curr = elements.Count - 1;
                }
                Text = String.Format("{0,3}", elements[curr]);
            }
        }

        public override void Draw(GameScreen screen, bool isSelected, GameTime gameTime)
        {
            SpriteBatch sp = screen.ScreenManager.SpriteBatch;

            Vector2 pos;
            int length = (int) screen.ScreenManager.Font.MeasureString(Text).X;

            pos.X = Position.X - length - length/2;
            pos.Y = Position.Y;
            sp.Draw(_left, pos, Color.White);
            base.Draw(screen, isSelected, gameTime);
            pos.X = Position.X + length + length / 2;
            sp.Draw(_right, pos, Color.White);

        }
    }
}
